<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-input
          v-model="query.defaultNumber"
          clearable
          placeholder="订单编号"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.operPersonName"
          clearable
          placeholder="操作人"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.inType"
          clearable
          placeholder="类型"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-select
          v-model="query.audit"
          class="filter-item"
          placeholder="請選擇審核狀態"
          style="width: 185px;"
          clearable
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in dict.audit"
            :key="item.id"
            :label="item.dictValue"
            :value="item.dictKey"
          />
        </el-select>
        <date-range-picker v-model="modelCreatedAt" class="date-item" :fix-date.sync="query.createTime" />
        <!-- 搜索 -->
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <template slot="left">
          <el-button
            class="filter-item"
            size="mini"
            type="primary"
            icon="el-icon-plus"
            @click="handleEdit"
          >
            新建入庫單
          </el-button>
          <el-button
            class="filter-item"
            size="mini"
            type="warning"
            icon="el-icon-s-check"
            :disabled="!crud.selections.length > 0"
            @click="audit"
          >
            審核
          </el-button>
        </template>
      </crudOperation>
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        style="width: 100%"
        stripe
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column
          type="selection"
          :row-key="getRowKeys"
        />
        <el-table-column
          prop="depot.name"
          label="仓库"
        />
        <el-table-column
          prop="defaultNumber"
          label="入库单号"
        />
        <el-table-column
          prop="vendor.name"
          label="供應商"
        />
        <el-table-column
          prop="audit"
          label="審核狀態"
        >
          <template slot-scope="scope">
            {{ dict.label.audit[scope.row.audit] }}
          </template>
        </el-table-column>
        <el-table-column
          prop="sumQuantity"
          label="入库总数量	"
        />
        <el-table-column
          prop="createdAt"
          label="创建时间"
        />
        <el-table-column
          prop="inTime"
          label="入库时间"
        />
        <el-table-column
          prop="operPersonName"
          label="操作员名字"
        />
        <el-table-column
          align="center"
          label="操作"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="success"
              @click="showDetails(scope.row)"
            >详情
            </el-button>
            <el-button
              size="mini"
              type="primary"
              @click="handleEdit(scope.row)"
            >编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination />
    </div>
  </div>
</template>
<script>
import crudDepot from '@/api/wms/depotBillIn'
import pagination from '@crud/Pagination'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import CRUD, { crud, form, header, presenter } from '@crud/crud'
import DateRangePicker from '@/components/DateRangePicker'

const defaultForm = { name: null }
export default {
  name: 'PurchaseDeptos',
  components: { pagination, rrOperation, crudOperation, DateRangePicker },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['inBillType', 'audit'],
  cruds() {
    return CRUD({
      title: '采购单', params: { 'inType': 0, 'deleteStatus': false }, url: 'depotBillIn/page', sort: 'id:desc',
      crudMethod: { ...crudDepot }, optShow: {
        add: false,
        edit: false,
        del: true,
        download: false,
        reset: false
      }
    })
  },
  data() {
    return {
      modelCreatedAt: '', // 时间选择器显示时间
      ids: [],
      permission: {
        add: ['admin', 'product:add'],
        edit: ['admin', 'product:edit'],
        del: ['admin', 'product:del']
      },
      selectedData: [],
      productId: [],
      queryTypeOptions: [
        { key: 'nameCn', display_name: '中文商品名称' }
      ],
      dialogVisible: false,
      tableData: []
    }
  },
  activated() {
    this.crud.refresh()
  },
  methods: {
    getModiFyKey() {
      if (this.ids.length > 1) {
        this.$message.error('不能同时修改多条数据')
        return false
      }
      if (this.ids.length === 0) {
        this.$message.error('请选择修改的采购单')
        return false
      }
      return true
    },
    getRowKeys(row) {
      this.ids.push(row.id)
      return row.id
    },
    handleEdit(row) {
      const inBillId = row.id
      this.$router.push({
        path: 'creatPurchaseInBill/' + (inBillId || 'add') })
    },
    showDetails(row) {
      const defaultNumber = row.defaultNumber
      this.$router.push({
        path: '/wms/inBillDetails',
        query: { defaultNumber: defaultNumber }
      })
    },
    audit() {
      const ids = this.crud.selections.map(item => {
        return item.id
      })
      crudDepot.audit(ids).then(res => {
        this.$notify({
          title: res.code === 0 ? '成功' : '失敗',
          message: res.message,
          type: res.code === 0 ? 'success' : 'error'
        })
        this.crud.refresh()
      })
    }
  }
}
</script>

<style scoped>
.crud-opts {
  padding: 4px 0;
  display: -webkit-flex;
  display: flex;
  align-items: center;
}

.crud-opts .crud-opts-right {
  margin-left: auto;
}
</style>
